package com.djx.CRUD;

import com.djx.utils.MongoDBUtil;
import com.mongodb.BasicDBObject;
import com.mongodb.client.*;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;


public class Select {

    MongoDBUtil util = MongoDBUtil.getInstance();
    MongoDatabase database = util.getDatabase("d01");
    MongoCollection<Document> collection = database.getCollection("c1");

    /**
     * 获取d01 下c1 集合中全部数据
     */
    @Test
    public void TestAll() {
        FindIterable<Document> documents = collection.find();
        for (Document document : documents) {
            System.out.println("数据为:::" + document);
        }

        MongoCursor<Document> mongoCursor = documents.iterator();
        while (mongoCursor.hasNext()) {
            System.out.println("iterator.document = " + mongoCursor.next());
        }
        util.closeDB();
    }

    /**
     * 查询指定数据
     */
    @Test
    public void Test指定() {
        BasicDBObject basicDBObject = new BasicDBObject();
        basicDBObject.put("name", "姚乐乐2");
        basicDBObject.put("age", 22);
        basicDBObject.put("sex", "女2");
        basicDBObject.put("city", "石家庄2");
        FindIterable<Document> documents = collection.find(basicDBObject);
        for (Document document : documents) {
            System.out.println("数据为:::" + document);
        }
        util.closeDB();
    }

    /**
     * 单个筛选条件
     */
    @Test
    public void TestFilters() {

        Bson eq = Filters.eq("name", "敦佳星");
        Bson eq1 = Filters.eq("name", "李熙蕾");
        Bson eq2 = Filters.eq("name", "姚乐乐1");
        FindIterable<Document> documents = collection.find(eq2);
        for (Document document : documents) {
            System.out.println("*********************************");
            System.out.println("数据为:::" + document);

        }

        util.closeDB();
    }


    /**
     * 单个筛选条件 第一个
     */
    @Test
    public void TestFiltersFirstOne() {

        Bson eq = Filters.eq("name", "敦佳星");
        Bson eq1 = Filters.eq("name", "李熙蕾");
        Document first = collection.find(eq1).first();
        System.out.println("数据为:::" + first);
        util.closeDB();
    }


    /**
     * 多个筛选条件
     */
    @Test
    public void TestManyFilters() {

        List<Bson> bsons = new ArrayList<>();
/*        bsons.add(Filters.eq("name", "姚乐乐1"));
        bsons.add(Filters.eq("age", 21));*/
        bsons.add(Filters.eq("name", "敦佳星"));

        Bson and = Filters.and(bsons);

        FindIterable<Document> documents = collection.find(and);
        for (Document document : documents) {
            System.out.println("数据为:::" + document);
        }
        util.closeDB();
    }


    /**
     * 多个筛选条件 第一条
     */
    @Test
    public void TestManyFiltersFirstOne() {

        List<Bson> bsons = new ArrayList<>();
        bsons.add(Filters.eq("name", "敦佳星"));
        bsons.add(Filters.eq("age", 10));
        Bson and = Filters.and(bsons);
        Document first = collection.find(and).first();

        System.out.println("数据为:::" + first);
        util.closeDB();
    }
}

